package test.hbn;

import gr.data.dao.AddressDAO;
import gr.data.dao.TypeAddressDAO;
import gr.data.dao.UserDAO;
import gr.data.hbn.pojo.Address;
import gr.data.hbn.pojo.TypeAddress;
import gr.data.hbn.pojo.User;
import gr.data.hbn.pojo.UserType;
import gr.utility.GrLogger;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Transaction;

public class PopolaUser {
	private static Logger logger = GrLogger.getLogger(PopolaUser.class);
	
	public static void main(String[] args) {
		logger.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< inizio");
                
		/******  Per l'utilizzo senza la hibernate section factory  ********
		Configuration configuration = new Configuration(); 
		configuration.configure("/hibernate.cfg.xml");
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		********************************************************************/
		
		Date now;
		
//********************************************************************************************//		
		////// TIPI UTENTE ////////////////////////////////
		logger.debug("[TIPO UTENTE]:START");
		now = new Date();
		UserType userType;
		
		/******* LOGICA USANDO LA CLASSE DAO **********************************************
		UserTypeDAO daoTipoUtente = new UserTypeDAO();
		try{
		  userType = daoTipoUtente.getFromId(1);
		  logger.debug("Recuperato tipo utente :"+userType.getDescrizione());
		}catch(DAOException dao){
			logger.error("impossibile recuperare tipo utente :"+1);
		}
		//*********************************************************************************/
		
		//salvataggio
		/*String tipo_utente = "NORMAL";
		UserType userType = new UserType(tipo_utente,new Timestamp(now.getTime()));
		session.save(userType);
		logger.debug("Creato tipo utente :"+tipo_utente);*/
		
		//recupero per id
		/*Query q = session.createQuery("from UserType where id = :id");
		q.setInteger("id", 1);
		userType = (UserType)q.uniqueResult();
		logger.debug("Recuperato tipo utente :"+userType.getDescrizione());*/
		
		//modifica 
		/*userType.setDescrizione("ola ola");
		session.save(userType);
		logger.debug("Modificato tipo utente :"+userType.getDescrizione());*/
		
		//eliminazione
		/*session.delete(userType);
		  logger.debug("Eliminato tipo utente :"+userType.getDescrizione());*/
		
		logger.debug("[TIPO UTENTE]:END");
		////// FINE TIPI UTENTE ////////////////////////////
//********************************************************************************************//
		
		
		
//********************************************************************************************//		
		////// TYPE ADDRESS ///////////////////////////////
		/*logger.debug("[TIPO INDIRIZZO]:START");
		now = new Date();
		TypeAddress addressType = new TypeAddress("VENDITORE",new Timestamp(now.getTime()));
		session.save(userType);
		logger.debug("[TIPO INDIRIZZO]:END");*/
		
		logger.debug("[TIPO INDIRIZZO]:START");
		//TypeAddress addressType = new TypeAddress("Casa",null);
		
		
		/*prova DAO generato da hibernate in automatico
		 * TypeAddressDAO daoAddressType = new TypeAddressDAO();
		List<TypeAddress> lista = daoAddressType.findAll();
		Iterator<TypeAddress> i = lista.iterator();
		while(i.hasNext()) {
			TypeAddress type = (TypeAddress)i.next();
			logger.debug(type.getId());
			logger.debug(type.getDescrizione());
		}
		//daoAddressType.save(new TypeAddress("Domicilio"));
		lista = daoAddressType.findByProperty("id", 1);
		i = lista.iterator();
		while(i.hasNext()) {
			TypeAddress type = (TypeAddress)i.next();
			logger.debug(type.getId());
			logger.debug(type.getDescrizione());
		}	
		
		lista = daoAddressType.findAll();
		 i = lista.iterator();
			while(i.hasNext()) {
				TypeAddress type = (TypeAddress)i.next();
				logger.debug(type.getId());
				logger.debug(type.getDescrizione());
			}
		
		
		
		daoAddressType.save(new TypeAddress("abcNORMALE"));
		
		lista = daoAddressType.findAll();
		 i = lista.iterator();
			while(i.hasNext()) {
				TypeAddress type = (TypeAddress)i.next();
				logger.debug(type.getId());
				logger.debug(type.getDescrizione());
			}
		
		FINE PROVA DAO */
		
		logger.debug("[TIPO INDIRIZZO]:END");
		////// FINE TYPE ADDRESS ///////////////////////////////
//********************************************************************************************//
		

//********************************************************************************************//		
		//////  USER ///////////////////////////////
		logger.debug("[USER]:START");
		/*UserDAO userdao = new UserDAO();
		Transaction transaction = userdao.beginTransaction();
		userdao.save(new User("marco","dalicco","marcopogo81@gmail.com",new Timestamp(now.getTime()),"T"));
	
		List<User> lista  = userdao.findAll();
		Iterator<User> i = lista.iterator();
			while(i.hasNext()) {
				User user = (User)i.next();
				logger.debug(user.getId());
				logger.debug(user.getNome());
			}
		userdao.commit(transaction);*/
		logger.debug("[USER]:END");
		////// FINE  USER ///////////////////////////////
//********************************************************************************************//	
//********************************************************************************************//		
		//////  ADDRESS ///////////////////////////////
		
		//cerco un utente
		UserDAO userdao = new UserDAO();
		List userList = (List)userdao.findByCognome("dalicco");
		Iterator iter = userList.iterator();
		User user=null;
		while(iter.hasNext()) {
			user = (User)iter.next();
			logger.debug(user.getId());
			logger.debug(user.getNome());
		}	
		
		
		//un tipo indirizzo
		TypeAddressDAO daoAddressType = new TypeAddressDAO();
		List lista = daoAddressType.findByProperty("id", 1);
		Iterator i = lista.iterator();
		TypeAddress typeAddress=null;
		while(i.hasNext()) {
			typeAddress = (TypeAddress)i.next();
			logger.debug(typeAddress.getId());
			logger.debug(typeAddress.getDescrizione());
		}	
		
		//imposto l'oggetto indirizzo e lo salvo
		AddressDAO addressdao = new AddressDAO();
		Transaction transaction = addressdao.beginTransaction();
		Address address = new Address (user,typeAddress,"TORINO","TO","IT","m.dalicco@kelyan.it");
		//addressdao.save(address);
	
		//li cerco tutti
		  lista  = userdao.findAll();
		 i = lista.iterator();
			while(i.hasNext()) {
			User user1 = (User)i.next();
				logger.debug(user1.getId());
				logger.debug(user1.getNome());
			}
		userdao.commit(transaction);
		
		//recupero l'indirizzo inserito.
		address = addressdao.findById(1);
		logger.debug("[address:id]"+address.getId());
		logger.debug("[address:citta]"+address.getCitta());
		
		//il tipo indirizzo
		typeAddress= address.getTypeAddress();
		logger.debug("[typeAddress:id]"+typeAddress.getId());
		logger.debug("[typeAddress:descrizione]"+typeAddress.getDescrizione());
		
		user = address.getUser();
		logger.debug("[user:id]"+user.getId());
		logger.debug("[user:nome]"+user.getNome());
		////// FINE  ADDRESS ///////////////////////////////
//********************************************************************************************//	
	
		/*UserDAO userDao = new UserDAO();
		User user = new User()
		userDao.save(new User() {
		});*/
		
		//tx.commit();
		//logger.debug("Committato? : " +tx.wasCommitted());
		
		
		
		/*
		 * List messages = session.createQuery("from User").list();
		logger.debug("Found " + messages.size() + " message(s):");
		Iterator i = messages.iterator();
		while(i.hasNext()) {
		User usr = (User)i.next();
		logger.debug(usr.getNome());
		logger.debug(usr.getId());
		}
		*/
		
		
		
		
		
		
		logger.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< fine");
	}
}